package com.ilab.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ilab.model.Department;
import com.ilab.model.Employee;
import com.ilab.service.DepartmentService;
import com.ilab.service.EmployeeService;
import com.ilab.web.forms.EmployeeForm;

public class EmployeeAction extends DispatchAction {

	private DepartmentService departmentService;
	private EmployeeService employeeService;
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	// 进入添加雇员页面
	public ActionForward addEmployeeUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		request.setAttribute("departmentList", departmentService.listDepartments());
		
		return mapping.findForward("goAddEmployeeUI");
	}

	// 处理添加雇员的请求
	public ActionForward addEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// 看看这里是否能够得到用户提交的各种数据
		EmployeeForm employeeForm = (EmployeeForm) form;
//		System.out.println(employeeForm.getEmail() + " " + employeeForm.getName() + " " + employeeForm.getPwd() + " " + employeeForm.getGrade() + " " + employeeForm.getSalary() + " " + employeeForm.getDepartmentId());
		
		// 创建一个雇员对象
		Employee employee = new Employee();
		employee.setEmail(employeeForm.getEmail());
		employee.setName(employeeForm.getName());
		employee.setGrade(Integer.parseInt(employeeForm.getGrade()));
		employee.setPwd(employeeForm.getPwd());
		employee.setSalary(Float.parseFloat(employeeForm.getSalary()));
		employee.setHiredate(new Date());
				
		// 该雇员是哪个部门的
		employee.setDepartment((Department) departmentService.findById(Department.class, employeeForm.getDepartmentId()));
		// 保存
		try {
			employeeService.add(employee);
		} catch (Exception e) {
			return mapping.findForward("opererr");
		}
		
		return mapping.findForward("operok");
		
	}
	
	public ActionForward listEmployeeUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 分页
		String s_pageNow = request.getParameter("pageNow");
		int pageNow = 1;
		
		if(s_pageNow != null) {
			pageNow = Integer.parseInt(s_pageNow);
		}
		
		request.setAttribute("emplist", employeeService.showEmployeeList(3, pageNow));
		
		int pageCount = employeeService.getPageCount(3);
		request.setAttribute("pageCount", pageCount);
		
		// 判断现在雇员级别
		Employee employee = (Employee) request.getSession().getAttribute("loginuser");
		if(employee.getGrade() == 1){
			return mapping.findForward("adminListEmployeeUI");
		}
		
		return mapping.findForward("goListEmployeeUI");
	}
	
	// 删除雇员
	public ActionForward deletEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 获取雇员的id
		String id = request.getParameter("id");
//		System.out.println("删除id： " + id);
		try {
			employeeService.deleteById(Employee.class, Integer.parseInt(id));
		} catch(Exception e) {
			return mapping.findForward("opererr");
		}
		
		return mapping.findForward("operok");
	}
	
	// 跳转到修改雇员页面
	public ActionForward updateEmployeeUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		
		Employee employee = (Employee) employeeService.findById(Employee.class, Integer.parseInt(id));
		
		request.setAttribute("employee", employee);
		request.setAttribute("departmentList", departmentService.listDepartments());
		
		return mapping.findForward("goUpdateEmployeeUI");
	}
	
	// 更新雇员信息
	public ActionForward updateEmployee(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 获取表单
		EmployeeForm employeeForm = (EmployeeForm) form;
		
		// employeeForm -> domain
		Employee employee = new Employee();
		employee.setId(Integer.parseInt(employeeForm.getId()));
		employee.setName(employeeForm.getName());
		employee.setEmail(employeeForm.getEmail());
		employee.setGrade(Integer.parseInt(employeeForm.getGrade()));
		employee.setSalary(Float.parseFloat(employeeForm.getSalary()));
		employee.setPwd(employeeForm.getPwd());
		
		employee.setDepartment((Department) departmentService.findById(Department.class, employeeForm.getDepartmentId()));
		
		try {
			employeeService.update(employee);
		} catch (Exception e) {
			return mapping.findForward("opererr");
		}
		
		return mapping.findForward("operok");
	}
}
